const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../services/user.js');

var app = getApp();
var pageNo = 1;
var hadLastPage = false;
var assembleItemStatus = '';
var isLoad = 0;

Page({
  data: {
    ordercateList: [{
      id: '0',
      name: '全部',
      selected: true
    }, {
      id: '1',
      name: '拼团中',
      selected: false
    }, {
      id: '2',
      name: '拼团成功',
      selected: false
    }, {
      id: '3',
      name: '拼团失败',
      selected: false
    }],
    assembleItemList: []
  },
  selectTag: function (index) {
    var ordercateList2 = this.data.ordercateList;
    for (var i = 0; i < ordercateList2.length; i++) {
      ordercateList2[i].selected = false;
    }
    ordercateList2[index].selected = true;
    this.setData({
      ordercateList: ordercateList2
    })
    if (index == '1') {
      assembleItemStatus = '0';
    } else if (index == '2') {
      assembleItemStatus = '1';
    } else if (index == '3') {
      assembleItemStatus = '2';
    } else if (index == '0') {
      assembleItemStatus = '';
    }
  },
  orderStateSelect: function (e) {
    if (isLoad == 1) {
      return;
    }
    const index = e.currentTarget.dataset.xuhao;
    this.selectTag(index);
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      assembleItemList: []
    });
    this.loadList();
  },
  onReady: function () {

  },
  onLoad: function () {
    var index = 0;
    this.selectTag(index);
  },
  onShow: function () {
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      assembleItemList: []
    });
    this.loadList();
  },
  onReachBottom: function () {
    this.loadList();
  },
  loadList: function () {
    isLoad = 1;
    if (hadLastPage) {
      isLoad = 0;
      return;
    }
    var that = this;
    var data = {
      pageNo: pageNo,
      assembleItemStatus: assembleItemStatus
    }
    util.request(api.AssembleItemList, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        var assembleItemList = that.data.assembleItemList;

        for (var i = 0; i < data.data.assembleItemList.length; i++) {
          assembleItemList.push(data.data.assembleItemList[i]);
        }

        if (data.data.pageNo == data.data.totalPages) {
          hadLastPage = true;
        } else {
          pageNo++;
        }

        that.setData({
          assembleItemList: assembleItemList
        });
        isLoad = 0;
      } else {
        console.log(data.content);
        isLoad = 0;
      }
    });
  }

})